package com.example.myapplication;

public class MainAdapter {

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

    public class MainActivity extends AppCompatActivity {

        private SwipeRefreshLayout mainSwipeRefreshLayout;
        private RecyclerView weathRecyclerView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mainSwipeRefreshLayout = findViewById(R.id.mainSwipeRefreshLayout);
            weathRecyclerView = findViewById(R.id.weathRecyclerView);
            //设置转圈的颜色
            mainSwipeRefreshLayout.setColorSchemeColors(
                    Color.parseColor("#FF0000"), Color.parseColor("#00FF00"),
                    Color.parseColor("#0000FF"), Color.parseColor("#2894FF")
            );
            //监听下拉刷新
            mainSwipeRefreshLayout.setOnRefreshListener(() -> {
                //一定要在此处设置下拉刷新的状态为假，否则空间会一直处在刷新状态
                getData();
            });
            //监听RecyclerView滚动事件
            weathRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.d("TAGS", dx + "|" + dy);
                    //dy的值大于0代表正在往下滑
                    if (dy > 0) {
                        //dy的值需要大于0才有机会滑动底部
                        if (recyclerView.canScrollVertically(1) == false) {
                            //如果已经不能在滑动了，就代表已经到底部了
                            Log.d("TAGS", "我已经到底部了");
                            //继续往下写加载更多代码
                        }
                    }
                }
            });
            getData();
        }

        private void getData() {
            OkGo.<String>get("https://yiketianqi.com/api?version=v3&appid=65716696&appsecret=pPQF7EgL")
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            WeathBean bean = new Gson().fromJson(response.body(), WeathBean.class);
                            WeathAdapter adapter = new WeathAdapter(bean.getData());
                            weathRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                            weathRecyclerView.setAdapter(adapter);
                            mainSwipeRefreshLayout.setRefreshing(false);//取消刷新状态
                        }
                    });
        }
    }

}
